package dk.itu.pam.rest;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import dk.itu.pam.model.Pizza;


// Will map the resource to the URL pizzas
@Path("/pizzas")
public class PizzasResource extends HttpServlet {

	// Allows to insert contextual objects into the class, 
	// e.g. ServletContext, Request, Response, UriInfo
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	@javax.ws.rs.core.Context 
	ServletContext c;
	@Context
	HttpServletRequest hsr;


	// Return the list of todos to the user in the browser
	@GET
	@Produces(MediaType.TEXT_XML)
	public List<Pizza> getPizzasBrowser() {
		Map parameters = hsr.getParameterMap();
		
		List<Pizza> pizzas = new ArrayList<Pizza>();
		List<Pizza> toBeRemoved = new ArrayList<Pizza>();
		ArrayList<Pizza> contextPizzas = (ArrayList<Pizza>) c.getAttribute("pizzas");
		pizzas.addAll(contextPizzas);
		
		if (parameters.containsKey("minprice")) {
			String min = (String) hsr.getParameter("minprice");
			Float minF = new Float(min);
			
			for (int i=0; i<pizzas.size(); i++) {
				Pizza p = pizzas.get(i);
				if (p.getPriceFloat()  < minF) {
					toBeRemoved.add(p);
				}
			}
			
		}
		if (parameters.containsKey("maxprice")) {
			String max = (String) hsr.getParameter("maxprice");
			Float maxF = new Float(max);
			
			for (int i=0; i<pizzas.size(); i++) {
				Pizza p = pizzas.get(i);
				if (p.getPriceFloat() > maxF) {
					toBeRemoved.add(p);
				}
			}
			
		}
		pizzas.removeAll(toBeRemoved);
		return pizzas;
	}
	
	// Return the list of todos for applications
	@GET
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public List<Pizza> getPizzas() {
		Map parameters = hsr.getParameterMap();
		
		List<Pizza> pizzas = new ArrayList<Pizza>();
		List<Pizza> toBeRemoved = new ArrayList<Pizza>();
		ArrayList<Pizza> contextPizzas = (ArrayList<Pizza>) c.getAttribute("pizzas");
		pizzas.addAll(contextPizzas);
		
		if (parameters.containsKey("minprice")) {
			String min = (String) hsr.getParameter("minprice");
			Float minF = new Float(min);
			
			for (int i=0; i<pizzas.size(); i++) {
				Pizza p = pizzas.get(i);
				if (p.getPriceFloat()  < minF) {
					toBeRemoved.add(p);
				}
			}
		}
		if (parameters.containsKey("maxprice")) {
			String max = (String) hsr.getParameter("maxprice");
			Float maxF = new Float(max);
			
			for (int i=0; i<pizzas.size(); i++) {
				Pizza p = pizzas.get(i);
				if (p.getPriceFloat()  > maxF) {
					toBeRemoved.add(p);
				}
			}
			
		}
		pizzas.removeAll(toBeRemoved);
		return pizzas;
	}
	
	
	// retuns the number of todos
	// Use http://localhost:8080/de.vogella.jersey.todo/rest/todos/count
	// to get the total number of records
	@GET
	@Path("count")
	@Produces(MediaType.TEXT_PLAIN)
	public String getCount() {
		ServletContext c = getServletContext();
		ArrayList<Pizza> contextPizzas = (ArrayList<Pizza>) c.getAttribute("pizzas");
		int count = contextPizzas.size();
		return String.valueOf(count);
	}

}
